package nuffzone;

import java.util.*;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;

public class messageManager {
	private static final messageManager instance = new messageManager();
	private static final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

	
	
		private messageManager(){
		}
		
		public static messageManager getInstance() {
			return instance;
		}
		
		public void createMessage(String username, String messageText, Date date) {
			
			Entity message = null;

				message = new Entity("Message");
				message.setProperty("Name", username);
				message.setProperty("Text", messageText);
				message.setProperty("Date", date);
				datastore.put(message);
		}
		
		public ArrayList<Message> getAllMessagesAsList(){
			
			ArrayList<Message> messageList = new ArrayList<Message>();
			
			Query messagesQuery = new Query("Message");
			List<Entity> results = datastore.prepare(messagesQuery).asList(
				    FetchOptions.Builder.withDefaults());
			for(Entity e : results)
			{
				String MUsername = (String) e.getProperty("Name");
				String MText = (String) e.getProperty("Text");
				Date MDate = (Date) e.getProperty("Date");
				Message m = new Message(MUsername, MText, MDate);
				
				messageList.add(m);
			}
			return messageList;
		}
}